//
//  HMEmoticonPageViewCell.swift
//  CZBKWB31
//
//  Created by Linkon Y on 16/8/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class HMEmoticonPageViewCell: UICollectionViewCell {
    
    
    //定义一个属性(一维数组) 外界调用
    var emoticons:[HMEmoticonModel]?{
        didSet{
            
            //先全部隐藏(cell复用问题)
            for btn in btnsArr{
              btn.hidden = true
            }
            
            //遍历HMEmoticonModel
            for(i,emoticonModel) in emoticons!.enumerate(){
                
                //获取btn
                let btn = btnsArr[i]
                //给btn的模型赋值
                btn.emoticonModel = emoticonModel
                //显示btn
                btn.hidden = false
                
               //判断是图片还是模拟表情
                if  emoticonModel.isEmoji{
                    let code = (emoticonModel.code ?? "" as NSString).emoji()
                    btn.setTitle(code, forState: UIControlState.Normal)
                    //(cell复用问题)
                    btn.setImage(nil, forState: UIControlState.Normal)
                }else{
                    btn.setImage(UIImage.emoticonImage(emoticonModel), forState: UIControlState.Normal)
                    //(cell复用问题)
                    btn.setTitle(nil, forState: UIControlState.Normal)
                }
            }
        }
    }
    
    var indexPath:NSIndexPath?{
        didSet{
            //设置text
//            messageLab.text = "第\(indexPath?.section ?? 0)组\n第\(indexPath?.item ?? 0)页"
        }
    }
    //定义一个可变的空数组
    var btnsArr: [HMEmoticonBtn] = [HMEmoticonBtn]()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
     // MARK: - 删除按钮监听
    func deleteBtnClick(){
    //发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(WBEMOTICONDELETEBTNNOTIFICATION, object: nil)
   
    }
    
     // MARK: - 20个按钮的点击事件
    func btnClick(btn:HMEmoticonBtn){
    //发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(WBEMOTICONEBTNNOTIFICATION, object: btn.emoticonModel)
   
    }  
    
    
    // MARK: - 设置视图
    private func setupUI(){
        //增加控件
        addChildBtns()
        contentView.addSubview(deleteBtn)

    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //计算btn的宽高
        let w = (self.frame.width - 10)/CGFloat(HMEmoticonsMaxCol)
        let h = (self.frame.height - 20)/CGFloat(HMEmoticonsMaxRow)
        //遍历btnArr  正向遍历
        for (index, button) in btnsArr.enumerate(){
            //设置Frame
            let col = CGFloat(index%HMEmoticonsMaxCol)
            let row = CGFloat(index/HMEmoticonsMaxCol)
            button.frame = CGRect(x: col*w + 5, y: row*h, width: w, height: h)
        }
        //给删除按钮添加Frame
        deleteBtn.frame = CGRect(x: self.frame.width - 5 - w, y: h*2, width: w, height: h)
    }
    
    //创建20个按钮
    func addChildBtns(){
        //循环创建btns
        for _ in 0..<HMEmoticonsMaxCount{
            let btn = HMEmoticonBtn()
            //添加点击事件
            btn.addTarget(self, action: "btnClick:", forControlEvents: UIControlEvents.TouchUpInside)
            //设置大小
            btn.titleLabel?.font = UIFont.systemFontOfSize(30)
            //增加到数组
            btnsArr.append(btn)
            //添加btn
            contentView.addSubview(btn)
        }
    }
    // MARK: - 懒加载控件
    private lazy var messageLab: UILabel = UILabel(textColor: UIColor.redColor(), textSize: 35, maxWidth: SCREENW)
    
    //删除按钮
    private lazy var deleteBtn: UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: "deleteBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        btn.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        return btn
    }()
}
